<template>
  <a-modal :maskClosable="false"
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model" :rules="validatorRules">

        <a-form-model-item label="桌位编号" required prop="tableNo">
          <a-input placeholder="请输入桌位编号" v-model="model.tableNo" />
        </a-form-model-item>
        <a-form-model-item label="区域名称" required prop="areaId">
          <a-select placeholder="请输入区域名称" v-model="model.areaId">
            <a-select-option v-for="item in areaList" :key="item.key" :value="item.key">{{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="类型名称" required prop="typeId">
          <a-select placeholder="请输入类型名称" v-model="model.typeId">
            <a-select-option v-for="item in typeList" :key="item.key" :value="item.key">{{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="排序" required prop="sort">
          <a-input-number placeholder="请输入排序" v-model="model.sort"  style="width: 200px"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, putAction, getAction, postAction } from '@/api/manage'

export default {
  name: 'ordStoreTableModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        tableNo: [{ required: true, message: '请输入桌位编号!' }],
        sort: [{ required: true, message: '请输入排序!' }],
        areaId: [{ required: true, message: '请输入区域!' }],
        typeId: [{ required: true, message: '请输入类型名称!' }]
      },
      url: {
        add: '/store/table',
        edit: '/store/table',
        areaList: '/store/table/area/selectList',
        typeList: '/store/table/type/selectList',
      },
      areaList: [],
      typeList: []
    }
  },
  created() {
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    const that = this
    getAction(this.url.areaList).then((res) => {
      if (res.success) {
        that.areaList = res.result
      }
    })
    getAction(this.url.typeList).then((res) => {
      if (res.success) {
        that.typeList = res.result
      }
    })
  },
  methods: {
    add() {
      this.model = Object.assign({}, {})
      this.model.sort=100;
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>

<style scoped>

</style>
